package com.tyut.musicdreamer.framework.starter.storage.config;

import com.aliyun.oss.OSS;
import com.tyut.musicdreamer.framework.starter.storage.service.StorageService;
import com.tyut.musicdreamer.framework.starter.storage.service.impl.AliyunOssStorageServiceImpl;
import com.tyut.musicdreamer.framework.starter.storage.service.impl.LocalStorageServiceImpl;
import com.tyut.musicdreamer.framework.starter.storage.task.StorageClassUpdateTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
 * 存储自动配置
 */
@Slf4j
@Configuration
@EnableConfigurationProperties(StorageProperties.class)
@EnableScheduling
public class StorageAutoConfiguration {
    
    /**
     * 配置阿里云OSS存储服务
     */
    @Bean
    @ConditionalOnClass({OSS.class})
    @ConditionalOnProperty(prefix = "musicdreamer.storage", name = "type", havingValue = "oss")
    @ConditionalOnMissingBean(StorageService.class)
    public StorageService ossStorageService(StorageProperties properties) {
        log.info("创建阿里云OSS存储服务");
        return new AliyunOssStorageServiceImpl(properties);
    }
    
    /**
     * 配置本地存储服务
     */
    @Bean
    @ConditionalOnProperty(prefix = "musicdreamer.storage", name = "type", havingValue = "local", matchIfMissing = true)
    @ConditionalOnMissingBean(StorageService.class)
    public StorageService localStorageService(StorageProperties properties) {
        log.info("创建本地存储服务");
        return new LocalStorageServiceImpl(properties);
    }
    
    /**
     * 配置存储类别更新任务
     */
    @Bean("storageClassUpdateTask")
    @ConditionalOnProperty(prefix = "musicdreamer.storage.task", name = "enable-storage-class-update", havingValue = "true", matchIfMissing = true)
    @ConditionalOnMissingBean(name = "storageClassUpdateTask")
    public StorageClassUpdateTask storageClassUpdateTask(StorageService storageService) {
        log.info("创建通用存储类别更新任务");
        return new StorageClassUpdateTask(storageService);
    }
} 